import { _decorator, Component, Node, Label, director } from 'cc';
import { SSS_Consts } from "../../../Script/Data/SSS_Consts";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import { SpiderSolitaire_GameStorage } from './SpiderSolitaire_GameStorage';
import { SpiderSolitaire_GameData } from './SpiderSolitaire_GameData';

const { ccclass, property } = _decorator;

@ccclass('SpiderSolitaire_Main_Scene')
export default class SpiderSolitaire_Main_Scene extends Component {
    public static Instance: SpiderSolitaire_Main_Scene;
    @property(Node)
    public Button_back: Node | null = null;
    @property(Node)
    public Button_3x3: Node | null = null;
    @property(Node)
    public Button_4x4: Node | null = null;
    @property(Node)
    public Button_5x5: Node | null = null;
    @property(Label)
    public Label_high_score_3x3: Label | null = null;
    @property(Label)
    public Label_high_score_4x4: Label | null = null;
    @property(Label)
    public Label_high_score_5x5: Label | null = null;
    onLoad() {
        SpiderSolitaire_Main_Scene.Instance = this;

        SpiderSolitaire_GameStorage.initData();
        this.ShowOn();
    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.destroy();
        this.RemoveListener();
    }
    Refresh() {
        this.Label_high_score_3x3.string = `${SpiderSolitaire_GameStorage.data[`mode_3x3`].high_score}`;
        this.Label_high_score_4x4.string = `${SpiderSolitaire_GameStorage.data[`mode_4x4`].high_score}`;
        this.Label_high_score_5x5.string = `${SpiderSolitaire_GameStorage.data[`mode_5x5`].high_score}`;
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);

        this.Button_3x3.on(Node.EventType.TOUCH_END, this.On_Button_3x3, this);
        this.Button_4x4.on(Node.EventType.TOUCH_END, this.On_Button_4x4, this);
        this.Button_5x5.on(Node.EventType.TOUCH_END, this.On_Button_5x5, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);

        this.Button_3x3.off(Node.EventType.TOUCH_END, this.On_Button_3x3, this);
        this.Button_4x4.off(Node.EventType.TOUCH_END, this.On_Button_4x4, this);
        this.Button_5x5.off(Node.EventType.TOUCH_END, this.On_Button_5x5, this);
    }
    On_Button_back() {
        console.log("On_Button_back---");
        this.ShowOff();
        director.loadScene(SSS_Consts.SceneName.MainScene);

        SSS_AudioManager.PlayButtonAudio();
    }
    async On_Button_3x3() {
        console.log("On_Button_3x3---");

        SpiderSolitaire_GameData.Game_mode = `mode_3x3`;

        director.loadScene(SSS_Consts.SceneName.S2048_Game_Scene);
        SSS_AudioManager.PlayButtonAudio();
    }
    async On_Button_4x4() {
        console.log("On_Button_4x4---");

        SpiderSolitaire_GameData.Game_mode = `mode_4x4`;

        director.loadScene(SSS_Consts.SceneName.S2048_Game_Scene);
        SSS_AudioManager.PlayButtonAudio();
    }
    async On_Button_5x5() {
        console.log("On_Button_5x5---");

        SpiderSolitaire_GameData.Game_mode = `mode_5x5`;

        director.loadScene(SSS_Consts.SceneName.S2048_Game_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }
}